using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MTRC.ODMS.DAL;
using MTRC.ODMS.DAL.SOM.CodeSetup;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// author:Benson
    /// </summary>
    public partial class StaffContractorEnquiry : BasePage_SOM
    {
        #region Property

        /// <summary>
        /// MenuID
        /// </summary>
        protected override string MenuID
        {
            get
            {
                return "M140823";
            }
        }

        protected int DetailLength
        {
            get
            {
                if (ViewState["DetailLength"] != null)
                {
                    return int.Parse(ViewState["DetailLength"].ToString());
                }
                else
                {
                    return 20;
                }
            }
            set
            {
                ViewState["DetailLength"] = value;
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(ChangeLanguage_Function);
            this.grdSearchResult.NeedDataBind += new APJ.Web.UI.WebControls.CommonGridView.NeedDataBindHandler(grdSearchResult_NeedDataBind);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grdSearchResult.PageSize = this.GridViewPageDefaultSize();
                //set control text about multi-language of page
                SetLanguage();
                //Custom element initial state of page 
                CustomInitializtion();
                btnSearch_Click(null, EventArgs.Empty);
            }
        }

        #endregion

        #region Event

        void ChangeLanguage_Function(object sender, EventArgs e)
        {
            //set control text about multi-language of page
            
            SetLanguage();
            if (IsPostBack) 
            {
                CustomInitializtion();
            }
            //bind the seach result
            BindSearchResult();
        }    

        protected void btnSearch_Click(object sender, EventArgs e)
        {            
            BindSearchResult();
        }

        /// <summary>
        /// sorting ,then afresh databind
        /// </summary>
        private void grdSearchResult_NeedDataBind()
        {
            BindSearchResult();
        }

       
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            this.Response.Redirect(APJ.Util.Utility.GetHttpHost(this.Request) + "/SOM/CodeSetup/StaffContractorEntry.aspx?Action=new");
        }

        protected void grdSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
            {                

            }
        }

        #endregion

        /// <summary>
        /// deal the edit event of gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == ("edit").ToUpper())
            {
                this.Response.Redirect("StaffContractorEntry.aspx?action=edit&StationEventID=" + e.CommandArgument.ToString());
            }
        }
        #region Method

        /// <summary>
        /// set page element text multi-language
        /// </summary>
        private void SetLanguage()
        {
            this.lblStationLable.Text = this.GetLabel("SOM_Station", true);
            this.btnSearch.Text = this.GetLabel("COM_Search");
            this.btnAddNew.Text = this.GetLabel("COM_CreateNew");
            this.grdSearchResult.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);

            this.lblDescription.Text = this.GetLabel("COM_Description");
            this.lblShowObsolete.Text = this.GetLabel("OPD_LocationTree_SelectView_cbObsolete", true);

            //this.grdSearchResult.Columns[0].HeaderText = GetLabel("SOM_LocalContractorDesc");
            //this.grdSearchResult.Columns[1].HeaderText = GetLabel("SOM_Void");
            //this.grdSearchResult.Columns[2].HeaderText = GetLabel("COM_Edit");
        }

        /// <summary>
        /// Custom element initial state of page
        /// </summary>
        private void CustomInitializtion()
        {
            FunctionItemSetting();

            if (DAL.Security.User.CanFunctionItemAccess("M140823_F2", this.ODMUser.UserProfileID, this.MenuID))
            {
                this.btnAddNew.Enabled = false;
            }
            else
            {
                this.btnAddNew.Enabled = true;
            }
            DetailLength = int.Parse(MTRC.ODMS.DAL.CommonData.GetNumVal("SOM_DetailLength").ToString());
        }

        /// <summary>
        /// bind the search result for page
        /// </summary>
        private void BindSearchResult()
        {

            DataTable dtSearchResult = GetSearchResult().Tables[0];
            this.grdSearchResult.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
            grdSearchResult.DataSource = dtSearchResult.DefaultView;
            grdSearchResult.DataBind();
        }

        /// <summary>
        /// search DB get data table Collections
        /// </summary>
        /// <returns>get data table of search result</returns>
        private DataSet GetSearchResult()
        {
            string Desc=txtDescription.Text;
            bool obsolete=cblShowObsolete.Checked;
            string stationIDs = "";
            this.SetStationSelectedItem(ref stationIDs, this.ddlStation);
            DataSet dsEnquiry = MTRC.ODMS.BLL.SOM.CodeSetup.StaffContractorBLL.GetStaffContractorSearch
                (
                stationIDs,
                Desc,
                obsolete
                );
            return dsEnquiry;
        }

        /// <summary>
        /// setting user access by function item.
        /// </summary>
        private void FunctionItemSetting()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140823_F1", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else if (DAL.Security.User.CanFunctionItemAccess("M140823_F3", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //BindStation
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
                this.ddlStation.Enabled = false;
            }
            //if (DAL.Security.User.CanFunctionItemAccess("M140101_F3", this.ODMUser.UserProfileID, this.MenuID)) //readonly
            //{
            //    this.btnAddNew.Enabled = false;
            //}

            //update by gordon 20151020 U247
            //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null)
            if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null)
            {
                this.ddlStation.SelectedValue = this.CurrentLocation.ToString();
                //this.ddlStation.SelectedIndex = 0;
            }
        }

        #endregion
    }
}
